"""
Case Type   : bugfix
Case Name   : 兼容B库下，支持alter table add(col type), add(col type) 语法
Create At   : 2025/6/11
Owner       : liu-tong-8848
Description :
    1、建表
    2、alter table add(col type), add(col type)方式增加多列
    3、alter table add(col type), add(col type)方式增加多列，同时结合增加索引语句使用
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:建表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fixed_108;create table t_fixed_108(a int);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)

        text = '----step2:alter table add(col type), add(col type)方式增加多列；expect:成功----'
        self.log.info(text)
        sql_cmd = "alter table t_fixed_108 add column (f1 int, f3 int), add column (f2 int);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_TABLE_MSG, sql_res, '执行失败' + text)
        sql_cmd = "\d+ t_fixed_108"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('f1', sql_res, '执行失败' + text)
        self.assertIn('f2', sql_res, '执行失败' + text)
        self.assertIn('f3', sql_res, '执行失败' + text)

        text = '----step3:alter table add(col type), add(col type)方式增加多列，同时结合增加索引语句使用；expect:成功----'
        self.log.info(text)
        sql_cmd = "alter table t_fixed_108 add column (f4 varchar(10), f6 timestamp), " \
                  + "add column (f5 int), add key i_fixed_108(f5);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_TABLE_MSG, sql_res, '执行失败' + text)
        sql_cmd = "\d+ t_fixed_108"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('f4', sql_res, '执行失败' + text)
        self.assertIn('f5', sql_res, '执行失败' + text)
        self.assertIn('f6', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fixed_108;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
